﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="">
//   
// </copyright>
// <summary>
//   The program.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace RedisMailTool
{
    using System;

    using Abp;
    using Abp.Dependency;
    using Abp.Net.Mail;

    using StackExchange.Redis;

    /// <summary>
    /// The program.
    /// </summary>
    internal class Program
    {
        /// <summary>
        /// The redis client.
        /// </summary>
        private static readonly ConnectionMultiplexer redisClient = ConnectionMultiplexer.Connect("localhost");

        /// <summary>
        /// The main.
        /// </summary>
        /// <param name="args">
        /// The args.
        /// </param>
        private static void Main(string[] args)
        {
            var startup = AbpBootstrapper.Create<SendMailModule>();
            startup.Initialize();

            // 测试是否可以正常发送
            var emailSender = IocManager.Instance.Resolve<IEmailSender>();
            emailSender.Send("目标地址", "主题", "内容");

            var sub = redisClient.GetSubscriber();
            sub.Subscribe(
                "mailtool", 
                (channel, message) =>
                    {
                        // 假设邮件格式
                        // 收件人地址#主题#内容
                        Console.WriteLine(message);
                        var recMsg = (string)message;
                        var msg = recMsg.Split('#');

                        emailSender.Send(msg[0], msg[1], msg[2]);
                    });
            sub.Publish("mailtool", "目标地址#主题#内容");

            Console.ReadKey();
        }
    }

}